package com.liang.bishi.nj;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 第三题：寻找唯一重复数
 * [4,2,1,3,3]
 * 3
 */
public class Problem03 {
    public static void main(String[] args) {
        ArrayList<Integer> nums = new ArrayList<>(Arrays.asList(4, 2, 1, 3, 3));
        System.out.println(findRepeatNum(nums));
    }

    public static int findRepeatNum(ArrayList<Integer> nums) {
        Map<Integer, Integer> countMap = new HashMap<>();
        int maxCount = 0;
        int result = 0;

        for (Integer num : nums) {
            countMap.put(num, countMap.getOrDefault(num, 0) + 1);
            if (countMap.get(num) > maxCount) {
                maxCount = countMap.get(num);
                result = num;
            }
        }
        return result;
    }
}
